package edu.nmt.cs.cse326.team4.crap;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ListViewActivity extends Activity implements OnClickListener {

	private ArrayList<Toilet> toilets;
	ToiletListAdapter adapter; // toilet list view adapter
	SharedPreferences prefs;
	
	private static Context context;
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_listview);
		
		context = this;
		// get shared prefs
		prefs = getSharedPreferences("crap", 0);
		SettingsActivity.view = "list";
		Bundle extras = getIntent().getExtras();
		
//		toilets = DBAdapter.getNearbyToilets(ViewMapActivity.getOrigin(), PreferenceManager.getDefaultSharedPreferences(this));
		toilets = (ArrayList<Toilet>) extras.getSerializable("toilets");
		//new ViewMapActivity.GetNearbyToilets().execute();
		
		
		Log.d("listview",Integer.toString(toilets.size()));
		if (toilets != null)
			showToilets();
	}

	private void showToilets() {
		adapter = new ToiletListAdapter((Activity) context, toilets);
		ListView listView = (ListView) findViewById(R.id.list);
		listView.setAdapter(adapter);
		// setListAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent i = new Intent(context, ViewDetailsActivity.class);
				i.putExtra("toilet", toilets.get(position));
				context.startActivity(i);
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		if (prefs.getString("user", null) != null)
			getMenuInflater().inflate(R.menu.list_view, menu);
		else
			getMenuInflater().inflate(R.menu.unreg_list_view, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_settings:
			Intent intent = new Intent(this, SettingsActivity.class);
			startActivity(intent);
			finish();
			return true;
		case R.id.map_view:
			intent = new Intent(this, ViewMapActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
			return true;
		case R.id.login:
			intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
			finish();
		case R.id.logout:
			prefs.edit().putString("user", null).commit();
			intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
			finish();
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onClick(View v) {
		LinearLayout toiletLayout = (LinearLayout) v;
		TextView name = (TextView) ((LinearLayout)(toiletLayout.getChildAt(0))).getChildAt(0);
		
		Intent intent = new Intent(this, ViewDetailsActivity.class);
		intent.putExtra("toilet", getToilet(name.getText().toString()));
		startActivity(intent);
		finish();
		
	}
	
	public Toilet getToilet(String name){
		for (Toilet t: toilets){
			if (t.getName().equals(name)){
				return t;
			}
		}
		return null;
	}
	
	

}
